#pragma once
#include "HttpHead.h"
#include "DataArray.h"
#include "FileMap.h"
#include "MapAdmin.h"

class CHttpSer
{
public:
    CHttpSer(void);
    ~CHttpSer(void);

    bool Start(unsigned short port);
    static CHttpSer* pHttp;
    bool GetStartTime(__int64 &start,__int64 &end,char *pStart);
    int GetRangeOffset(__int64 &start,__int64 &end,char *pRange);
    bool DelFileMapNode(const char* filename);
    bool DelAllFileMapNode();
    static UINT WINAPI SvrThread(PVOID pParam);
	static UINT WINAPI WorkThread(PVOID pParam);
    void AnalyticGET(SOCKET sock,char* pRevc);
    void AnalyticPOST(SOCKET sock,char* pRevc);
    void AnalyticAdmin(SOCKET sock,char* pRevc,int revlen);
    void socket_close( int fd );
    bool SetTimerOut(SOCKET sock);
	void RangeBytes(SOCKET,FileMap*,char*);

private:
    bool socket_init(unsigned short port);
    void socket_end();

	void SendIndexHtml(SOCKET sock,bool bwap = false);
	int GetIP(SOCKET sock,char* ip);
	int IsLan(SOCKET sock);
    int GetFileName(char* InBuf,char* OutBuf);
    int SendFileInfo(char* FileName,SOCKET sock,__int64 size);
	int SendRangeInfo(char* FileName,SOCKET sock,__int64 start,__int64 end,__int64 size);
    int SendFile(FileMap  *pFmap,SOCKET sock,__int64 startoffset = 0,__int64 endoffset = 0);///?????C??????????4G????
    int IsSuffixEqual(char root[],char suffix[]);
    void get_filetype(char *filename, char *filetype);
   //CDataArray  m_ThreadList;
    CDataArray  m_FileList;
	FileMap *GetMapNode(const char* filename);
    int m_socket;
	unsigned short m_port;
	bool InitDB();
	
    CRITICAL_SECTION m_cs;
	int m_nSpeed;

};

/*
??Range??????????????????????
?????500????bytes=0-499
???????500???bytes=500-999
??????500????bytes=-500
????500????????bytes=500-
?????????????bytes=0-0,-1
???????????bytes=500-600,601-999
*/
